#include "bsp.h"
#include "gd32f30x.h"

/* retarget the gcc's C library printf function to the USART */
#include <errno.h>
#include <sys/unistd.h> // STDOUT_FILENO, STDERR_FILENO
int _write(int file, char *data, int len)
{
	if ((file != STDOUT_FILENO) && (file != STDERR_FILENO)) {
		errno = EBADF;
		return -1;
	}

	for (int i = 0; i < len; i++) {
		usart_data_transmit(USART2, (uint8_t)data[i]);
		while (RESET == usart_flag_get(USART2, USART_FLAG_TBE))
			;
	}

	// return # of bytes written - as best we can tell
	return len;
}

